import { Vec3 } from 'cc';
import { view } from 'cc';
import { _decorator, Component, Camera, Node } from 'cc';
import { eliminateGameManager } from './EliminateGameManager';
import { EliminateMode } from './EliminateConst';
const { ccclass, property } = _decorator;

@ccclass('OrthographicCameraAdapter')
export class OrthographicCameraAdapter extends Component {

    @property(Node)
    topBoard: Node = null;

    @property(Node)
    topBoardV: Node = null;

    @property(Camera)
    cameraUIMain: Camera = null;

    @property(Camera)
    cameraToast: Camera = null;

    @property(Node)
    slot: Node = null;

    @property(Node)
    slotTrigger: Node = null;

    @property(Node)
    canvasNode: Node = null;

    @property(Node)
    uiTest: Node = null;

    baseHeight: number = 20;

    onLoad() {
        this.baseHeight = this.node.getComponent(Camera).orthoHeight;
    }

    start() {
        view.on('canvas-resize', this.adjustCamera, this);
        this.adjustCamera();
        const cameraComponent = this.getComponent(Camera);
        // @ts-ignore
        cameraComponent._camera.update();

        this.schedule(() => {
            const slotWorldPos = new Vec3();
            this.slot.getWorldPosition(slotWorldPos);
            console.log('slotWorldPos is ', slotWorldPos);

            eliminateGameManager.uiManager.slotsNode.leftPos.z = this.slot.getWorldPosition().z;
            eliminateGameManager.uiManager.slotsNode.rightPos.z = this.slot.getWorldPosition().z;

            const out2 = new Vec3();
            cameraComponent.convertToUINode(slotWorldPos, this.canvasNode, out2);
            console.log("out2 is ", out2);
            this.uiTest.setPosition(out2);

            eliminateGameManager.uiManager.setRingNodePostion(new Vec3(out2.x, out2.y, 0));
            eliminateGameManager.game.clearPart.node.setPosition(new Vec3(out2.x, out2.y, 0));
        }, 0.01, 10);
    }

    adjustCamera() {
        const cameraComponent = this.getComponent(Camera);
        const designSize = view.getDesignResolutionSize();
        if (cameraComponent) {
            // cameraComponent.camera.screenScale
            let canvasSize = view.getVisibleSize();
            const baseRatio = designSize.width / designSize.height;
            console.log("baseRatio", baseRatio);
            const viewRatio = canvasSize.width / canvasSize.height;
            console.log("viewRatio", viewRatio);

            console.log("canvas size is ", canvasSize);

            const designHWRatio = designSize.height / designSize.width;
            const realWHRatio = canvasSize.height / canvasSize.width;

            const odds = realWHRatio / designHWRatio;
            console.log("odds", odds);

            const offsetZ = canvasSize.height / designSize.height * 30

            if (eliminateGameManager.gameMode == EliminateMode.MATCH2) {
                const topBoardPos = this.topBoard.getWorldPosition();
                const slotPos = this.slot.getWorldPosition();
                this.slot.setWorldPosition(slotPos.x, slotPos.y, slotPos.z * odds);

                this.topBoard.setWorldPosition(topBoardPos.x, topBoardPos.y, this.slot.getWorldPosition().z - offsetZ - 36);
                this.topBoardV.setWorldPosition(topBoardPos.x, topBoardPos.y, this.slot.getWorldPosition().z - offsetZ - 36);
            }

            const slotTriggerPos = this.slotTrigger.getWorldPosition();
            this.slotTrigger.setWorldPosition(slotTriggerPos.x, slotTriggerPos.y, slotTriggerPos.z * odds);

            cameraComponent.orthoHeight = view.getVisibleSize().height / 2;

            this.cameraToast.orthoHeight = view.getVisibleSize().height / 2;

        }
    }

    protected update(dt: number): void {
        // let out1 = new Vec3();
        // const slotWorldPos = new Vec3();
        // this.slot.getWorldPosition(slotWorldPos);
        // console.log('slotWorldPos is ', slotWorldPos);

        // const slotScreenPos = this.getComponent(Camera).worldToScreen(slotWorldPos, out1);
        // console.log("out1 is ", out1);

        // const out2 = new Vec3();

        // this.getComponent(Camera).convertToUINode(slotWorldPos, this.canvasNode, out2);
        // console.log("out2 is ", out2);

        // const resPos = this.uiTest.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(slotScreenPos.x, slotScreenPos.y, 0));
        // console.log("resPos", resPos);
        // this.uiTest.setPosition(resPos);

    }
}
